/*
 * 将值val保存在以key为标记的数组中
 */
function smb_InsertIntoArray(key, val) {
    var jsonArray = appcan.locStorage.getVal(key);
    var arr = undefined;
    if (jsonArray)
        arr = JSON.parse(jsonArray);
    else
        arr = [];
    if ($.isArray(val)) {
        for (var index = 0; index < val.length; index++) {
            var _val = val[index];
            var _index = arr.indexOf(_val);
            if (_index != -1)
                arr.splice(_index, 1);
            arr.push(_val);
        }
    } else {
        var _index = arr.indexOf(val);
        if (_index != -1)
            arr.splice(_index, 1);
        arr.push(val);
    }
    appcan.locStorage.setVal(key, JSON.stringify(arr));
}

/*
 * 获取以key为标记的数组（cbFilter为undefined时返回所有）
 */
function smb_GetStorageArray(key, cbFilter) {
    var jsonArray = appcan.locStorage.getVal(key);
    var arr = undefined;
    if (jsonArray) {
        arr = JSON.parse(jsonArray);
    } else
        arr = [];
    if ($.isArray(arr)) {
        if (cbFilter) {
            var _arr = [];
            for (var i = 0; i < arr.length; i++) {
                if (cbFilter(arr[i]))
                    _arr.push(arr[i]);
            }
            return _arr;
        } else
            return arr;
    } else
        return [];
}

/*
 * 移除以key为标记的数组中项
 */
function smb_RemoveFromArray(key, val) {
    var jsonArray = appcan.locStorage.getVal(key);
    var arr = undefined;
    if (jsonArray)
        arr = JSON.parse(jsonArray);
    else
        arr = [];
    if ($.isArray(arr)) {
        var index = arr.indexOf(val);
        if (index != -1) {
            arr.splice(index, 1);
        }
    }
    appcan.locStorage.setVal(key, JSON.stringify(arr));
}

/*入栈*/
function smb_PushStack(key, val) {
    var jsonArray = appcan.locStorage.getVal(key);
    var arr = undefined;
    if (jsonArray)
        arr = JSON.parse(jsonArray);
    else
        arr = [];
    arr.push(val);
    appcan.locStorage.val(key, JSON.stringify(arr));
}

/*出栈*/
function smb_PopStack(key) {
    var jsonArray = appcan.locStorage.getVal(key);
    var arr = undefined;
    if (jsonArray)
        arr = JSON.parse(jsonArray);
    else
        arr = [];
    var val = undefined;
    if (arr.length > 0)
        val = arr.pop();
    appcan.locStorage.setVal(key, JSON.stringify(arr));
    return val;
}

/*
 设置用户数据,注销时将清空
 */
function smb_CacheStorageVal(key, val) {
    appcan.locStorage.val(key, val);
    smb_CacheStorageKey(key);
}

/*
 存储locStorage Key
 用于清理过期/无效值
 */
function smb_CacheStorageKey(storageKey) {
    if (storageKey) {
        smb_InsertIntoArray("$StorageCacheKeys", storageKey);
    }
}

function smb_ClearStorageKey(storageKey) {
    if (storageKey) {
        appcan.locStorage.remove(storageKey);
        smb_RemoveFromArray("$StorageCacheKeys", storageKey);
    }
}

/*
 Cache过storageKey的locStorage都将被清理
 */
function smb_ClearStorageCaches() {
    var storageCacheKeys = smb_GetStorageArray("$StorageCacheKeys");
    if (storageCacheKeys) {
        for (var i = 0; i < storageCacheKeys.length; i++) {
            smb_ClearStorageKey(storageCacheKeys[i]);
        }
    }
}